/*
 * @Author: sdorein sdorein@163.com
 * @Date: 2024-08-23 14:36:28
 * @LastEditors: sdorein sdorein@163.com
 * @LastEditTime: 2024-10-30 13:43:44
 * @FilePath: \YiShangBuiding\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// router/index.ts
import { createRouter, createWebHashHistory } from 'vue-router';
// createWebHashHistory
const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('../views/index.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/about.vue')
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('../views/test.vue')
  }
];

const subrouter = createRouter({
  history: createWebHashHistory(),// process.env.BASE_URL
  routes
});
// --定义路由放行数组，不需要登录的页面
// -- 路由守卫
subrouter.beforeEach((to, from, next) => {
  // 验证token，如果不存在，则跳转到登录页面,如果存在，
  console.log(to, from, next);
  
  next();
  return;
  if (to.path === '/login') {
    next();
  }else {
    const token = localStorage.getItem('token');
    if (!token) {
      next('/login');
    } else {
      next();
    }
  }
  next();
})
export default subrouter;